let data = [];
let rest_time = [];
let restTiming = true;
let timeout;
//倒计时函数
function restTime() {
    if(!restTiming){
        return;
    }
    timeout = setTimeout(restTime, 1000);
    for(let i = 0; i<data.length; i++){
        if(data[i].status === 0){
            rest_time[data[i].act_id] -= 1000;
            let seconds = rest_time[data[i].act_id];
            if(seconds <= 0){
                $('#activity').html('<p class="text-success" style="text-align: center;margin-top: 10px;">活动正在刷新，请稍候..</p>');
                restTiming = false;
                setTimeout(function(){
                    afterVerify();
                },2000);
                return;
            }
            let second = Math.floor(seconds/1000%60);
            let min = Math.floor(seconds/1000/60%60);
            let hour =Math.floor(seconds/1000/60/60%24);
            let day =Math.floor(seconds/1000/60/60/24);
            let formatDate = (day? (day + '天 '): '') + (hour > 9? hour: '0' + hour) + ':' + (min > 9? min: '0' + min) + ':' + (second > 9? second: '0' + second);
            $('#rest_time'+data[i].act_id).html(formatDate + ' 后截止');
        }
    }
}
//进度条定时器
function progressTimeout() {
    setTimeout(progressTimeout, 10000);
    for(let i = 0; i<data.length; i++){
        if(data[i].status === 0){
            let start_timestamp = data[i].start_timestamp;
            let end_timestamp = data[i].end_timestamp;
            let now = getTime();
            let total_time = end_timestamp - start_timestamp;
            let experienced_time = now - start_timestamp;
            let percent = Math.floor((experienced_time*100)/total_time);
            $('#progress'+data[i].act_id).attr('aria-valuenow',percent).css('width',percent+'%');
        }
    }
}

//主业务逻辑，需要在完成验证后执行
function afterVerify() {
    //加载抽奖信息
    $.post(baseUrl+'client/activity',{},function(result){
        afterRequest(result);
    },'json');
}

function afterRequest(result){
    if(result.code === 0){
        data = result.data;
        let html = '';
        for(let i = 0; i<data.length; i++){
            html += '<li><div class="act_div">';
            html += '<div><img src="'+ data[i].goods_imgs[0] +'" alt=""></div>';
            html += '<div class="act_detail">';
            html += '<p><span style="color: '+(data[i].status > 0 ? '#242842':'#ff4e00')+'">第'+ data[i].act_no +'期</span><span class="'+ (data[i].status > 0 ? 'status2':'') +'">'+ (data[i].status > 0 ? '已结束' : (data[i].draw_time+'开奖')) +'</span></p>';
            html += '<span></span>';
            html += '<p>'+ data[i].goods_name +'</p>';
            html += '<p><span>市场价: '+ data[i].price +'</span><span>数量：'+ data[i].number +'</span></p>';
            html += '</div></div>';
            //未开奖显示进度
            if(data[i].status === 0 ){
                data[i].end_timestamp = getTime(data[i].end_time);
                data[i].start_timestamp = getTime(data[i].start_time);
                html += '<div class="container more_detail"><div class="progress">';
                html += '<div class="progress-bar progress-bar-danger progress-bar-striped active" role="progressbar" id="progress'+data[i].act_id+'" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">';
                html += '</div></div>';
                html += '<p class="text-success"><span>'+data[i].count+'人已参与</span><span>中奖率: '+data[i].probability+'%</span><span id="rest_time'+data[i].act_id+'"></span>';
                html += '</p></div>';
            }
            html += '</li>';
        }
        $('#activity').html(html);
        //计算每个活动的剩余时间，统一计入变量
        let now = getTime();
        for(let i = 0; i<data.length; i++){
            if(data[i].status === 0 && data[i].end_timestamp > now){
                rest_time[data[i].act_id] = data[i].end_timestamp - now;
            }
        }
        //定时器
        restTiming = true;
        restTime();//剩余时间定时器
        progressTimeout();//进度条定时器
    }else{
        alert(result.msg);
    }
}

//查看详情
$('#activity').on('click','li',function(){
    let index = $(this).index();
    let act_id = data[index].act_id;
    location.href='./detail.html?act_id='+act_id;
})

